package com.wc.算法提高课.B第二章_搜索.DFS之剪枝与优化.生日蛋糕;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/9/29 15:37
 * @description https://www.acwing.com/problem/content/170/
 */
public class Main {
    /**
     * 思路：
     * V = /sum_1^m r[i] * r[i] * h[i]
     * S = /sum_1^m 2 * r[i] * h[i]
     *
     * v 表示前面所使用的体积, s 表示前面所使用的面积
     * 每一层的R, H的范围
     * r * r * h <= n - v, r <= sqrt(n - v), r[i] <= r[i + 1] - 1 => r[i] <= min(sqrt(n - v), r[i + 1] - 1)
     * h <= (n - v) / r / r, h[i] <= h[i + 1] - 1 => h[i] <= min((n - v) / r / r, h[i + 1] - 1)
     * 并且前面后面每一层最小的体积 minv[i] = r * r * h >= i * i * i;
     * 并且前面后面每一层最小的体积 mins[i] = 2 * r * h >= 2 * i * i;
     * v[depth] = /sum_1^depth r[i] * r[i] * h[i];
     * s[depth] = /sum_1^depth 2 * r[i] * h[i]
     * s[depth] = 2 / r[depth + 1] * /sum_1^depth r[i] * r[depth + 1] * h[i] >= 2 / r[depth + 1] * v[depth]
     * <p>
     * s[depth] >= 2 / r[depth + 1] * v[depth] = 2 / r[depth + 1] * (n - v)
     * <p>
     * s + s[depth] >= ans, 剪枝
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 25, INF = 0x3f3f3f3f;
    static int[] R = new int[N], H = new int[N];
    // minv[i]表示从前i层 最小的体积, mins前i层最小的侧面积表示每一层最小的侧面积
    static int[] minv = new int[N], mins = new int[N];
    static int n, m, ans = INF;

    public static void main(String[] args) {
        n = sc.nextInt();
        m = sc.nextInt();
        for (int i = 1; i <= m; i++) {
            // V = pi * r * r * h, 每一层最小就是他的层数 r = h = i
            minv[i] = minv[i - 1] + i * i * i;
            // S = 2 * pi * r * h
            mins[i] = mins[i - 1] + 2 * i * i;
        }
        R[m + 1] = H[m + 1] = INF;
        dfs(m, 0, 0);
        out.println(ans == INF ? 0 : ans);
        out.flush();
    }

    /**
     * @param u 从上往下， 第u层
     * @param v 前面所用体积
     * @param s 前面所用面积
     */
    static void dfs(int u, int v, int s) {
        if (v + minv[u] > n) return;
        if (s + mins[u] >= ans) return;
        if (s + 2 * (n - v) / R[u + 1] >= ans) return;
        if (u == 0) {
            if (n == v) ans = s;
            return;
        }

        for (int r = Math.min((int) Math.sqrt(n - v), R[u + 1] - 1); r >= u; r--) {
            for (int h = Math.min((n - v) / r / r, H[u + 1] - 1); h >= u; h--) {
                R[u] = r;
                H[u] = h;
                int t = 0;
                if (m == u) t = r * r;
                dfs(u - 1, v + r * r * h, s + 2 * r * h + t);
            }
        }
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
